<template>
  <div>
    <el-card>
      <h2>代码题目列表</h2>
      <el-table :data="questions" style="width: 100%">
        <el-table-column prop="id" label="ID" width="60"/>
        <el-table-column prop="title" label="题目标题"/>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button type="primary" size="small" @click="goToQuestion(scope.row.id)">答题</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const questions = ref([])
const router = useRouter()

onMounted(async () => {
  const res = await axios.get('/api/code-questions/')
  questions.value = res.data
})

function goToQuestion(id) {
  router.push({ name: 'code-edit-id', params: { questionId: id } })
}
</script>